from flask_script import Manager
from bbs import app
from exts import db
from flask_migrate import Migrate,MigrateCommand
from cms.models import admin,Role,cmsPermission
from front.models import FrontUser

manager=Manager(app)

Migrate(app,db)

manager.add_command('db',MigrateCommand)

@manager.option('-p','--password',dest="password")
@manager.option('-u','--username',dest="username")
@manager.option('-e','--email',dest="email")
def get(password,email,username):
    user=admin(password=password,email=email,username=username)
    db.session.add(user)
    db.session.commit()
    print('='*30)
    print("you are the best!")

@manager.option('-p','--password',dest="password")
@manager.option('-t','--telephone',dest="telephone")
@manager.option('-u','--username',dest="username")
def set_front(password,telephone,username):
    user=FrontUser(password=password,telephone=telephone,username=username)
    db.session.add(user)
    db.session.commit()
    print('添加成功了！')

# @manager.command
# def set_role():
#     visitor=Role(name="访问者",desc="这个是访问者的权限")
#     visitor.permission=cmsPermission.read
#     operator = Role(name="运营者", desc="这个是运营者的权限")
#     operator.permission=cmsPermission.ariticle|cmsPermission.read|cmsPermission.boader|cmsPermission.talk|cmsPermission.m_front
#     the_admin=Role(name="管理员",desc="这是管理员权限")
#     the_admin.permission=cmsPermission.m_front|cmsPermission.talk|cmsPermission.boader|cmsPermission.ariticle|cmsPermission.m_behind
#     develop=Role(name="开发者",desc="这是开发者的权限")
#     develop.permission=cmsPermission.all
#     db.session.add_all([visitor,operator,the_admin,develop])
#     db.session.commit()

# @manager.command
# def set_user_role():
#     user=admin.query.get(3)
#     role=Role.query.get(4)
#     if user:
#         role.admins.append(user)
#         db.session.commit()
#         print("加入成功了！")
#     else:
#         print("there is no such user!")
#
# @manager.command
# def check():
#     user=admin.query.first()
#     if user.has_permission(cmsPermission.read):
#         print("这个用户有读权限")
#     else:
#         print("这个用户没有读权限")


# @manager.command
# def check():
#     user=FrontUser.query.filter_by(telephone='18934084265').first()
#     db.session.delete(user)
#     db.session.commit()
#     print("成功了")
#     if user.has_permission(cmsPermission.read):
#         print("这个用户有读权限")
#     else:
#         print("这个用户没有读权限")

if __name__=='__main__':
    manager.run()